import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:shopping_group/res/colors.dart';
import 'base/page_constance.dart';
import 'event/event.dart';
import 'main/lz_hot_cakes/provider/lz_hot_cakes_provider.dart';
import 'main/lz_mall/provider/lz_mall_home_provider.dart';
import 'modle/main/ui/bottom_menu_page.dart';
import 'package:provider/provider.dart';

import 'utils/route_util.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  //强制竖屏
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
      .then((_) {
    runApp(MyApp());
  });
}

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    _initAsync();
  }

  void _initAsync() async {
    Colours.themeColor = Colours.greed_66c;
  }

  @override
  void dispose() {
    super.dispose();
  }

  //注册主题切换监听
  void registerThemeEvent() {
    eventBus
        .on<ThemeChangeEvent>()
        .listen((ThemeChangeEvent onData) => this.changeTheme(onData));
  }

  //刷新主题样式
  void changeTheme(ThemeChangeEvent onData) {
    setState(() {
      Colours.themeColor = onData.themeColor;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProvider(
            create: (context) => LzHotCakesProvider(),
          ),
          ChangeNotifierProvider(
            create: (context) => LzMallHomeProvider(),
          ),
        ],
        child: ChangeNotifierProvider<ThemeProvider>(
            create: (_) => ThemeProvider(),
            child: Consumer<ThemeProvider>(builder: (_, provider, __) {
              return MaterialApp(
                  builder: BotToastInit(),
                  navigatorKey: RouteUtil.navigatorKey,
                  routes: PageConstance.getRoutes(),
                  navigatorObservers: [BotToastNavigatorObserver()],
                  // home: BottomMenuPage(),
                  initialRoute: PageConstance.MAIN_PAGE,
                  onGenerateRoute: routes,
                  theme: ThemeData(primaryColor: Colours.themeColor),
                  localizationsDelegates: [
                    GlobalMaterialLocalizations.delegate,
                    GlobalWidgetsLocalizations.delegate,
                  ],
                  supportedLocales: [
                    const Locale('zh', 'CN'),
                    const Locale('en', 'US'),
                  ]);
            })));
  }
}

Route routes(RouteSettings settings) {
  if (settings.name == PageConstance.MAIN_PAGE) {
    return MaterialPageRoute(
      builder: (context) {
        return BottomMenuPage();
      },
    );
  }
  return null;
}

/**
    账号18059867366
    密码111111
 * 这里写个日报
 * - 9.14    
 * 1.屏蔽 推广 社区按钮 
 * 2.屏蔽“我的”里面的、积分、能量券、复购券、抽奖记录这几项；
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 1、把“首页”内容替换成之前小周已实现的那个利正农业的首页，首页上的内容点击不需要跳转，就是个静态的显示界面。
2、以下内容做隐藏处理：
  
  
3、所有调后台的，ip不变，端口改成另一个。
4、“我的”里面邀请码，那张图换掉，下方的二维码，对应的网址换掉。



中期来看（大概三四周时间）：
从新做个app，内容和现在的商城相同。



长期来看，2~3个月时间开发利正农业的功能，和利正商城在同个app里，内容现在还未完全确定。同时完善商城的功能，比如能够选商品规格、能够分类查找商品、我的里面多出一些营销工具。
 */

extension ThemeModeExtension on ThemeMode {
  String get value => ['System', 'Light', 'Dark'][index];
}

class ThemeProvider extends ChangeNotifier {
  ThemeProvider();

  swithEnvironment(String e) async {
    ///检测
    notifyListeners();
  }
}

class EcCartProvider extends ChangeNotifier {
  EcCartProvider();
}
